// JavaScript Document
var GameObject = Class.extend({
	x: 0,
	y: 0,
	init: function(x, y) {
		this.x = x;
		this.y = y;
	},
	setPos: function(x, y) {
		this.x = x;
		this.y = y;
	},
	draw: function() {
		try {  
            game.ctx.drawImage(content.images["TilesSprite2"], 0, 0, game.grid, game.grid, 
            	this.x, this.y, game.grid, game.grid);
        } catch(e) {alert(e)}
	},
	a: function() {
		alert("KKK");
	}
});

var Player = GameObject.extend({
	x_move: 0,
	y_move: 0,
	speed: 10,
	init: function() {
	},
	setPos: function(x_abs, y_abs) {
		this._super(x_abs * game.grid, y_abs * game.grid);
	},
	moveX: function(n) {
		this.x_move += n * game.grid;  
	},
	moveY: function(n) {
		this.y_move += n * game.grid;
	},
	isMoving: function() {
		return (this.x_move != 0 || this.y_move != 0);
	},
	getPosX: function() {
		return Math.ceil(this.x/game.grid);
	},
	getPosY: function() {
		return Math.ceil(this.y/game.grid);
	},
	update: function() {
		if (this.x_move > 0) {
			this.x_move -= this.speed;
			this.x += this.speed;
			if (this.x_move < 0) {
				this.x += this.x_move;
				this.x_move = 0;
			}
		}
		if (this.x_move < 0) {
			this.x_move += this.speed;
			this.x -= this.speed;
			if (this.x_move > 0) {
				this.x += this.x_move;
				this.x_move = 0;
			}
		}
		if (this.y_move > 0) {
			this.y_move -= this.speed;
			this.y += this.speed;
			if (this.y_move < 0) {
				this.y += this.y_move;
				this.y_move = 0;
			}
		}
		if (this.y_move < 0) {
			this.y_move += this.speed;
			this.y -= this.speed;
			if (this.y_move > 0) {
				this.y += this.y_move;
				this.y_move = 0;
			}
		}
	}
});